package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Menu;
import com.example.mapper.MenuMapper;
import com.example.service.IMenuService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CDHong.it
 * @since 2023-06-27
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Override
    public List<Menu> menuByRoleId(int roleId) {
        // 根菜单
        List<Menu> rootMenu = baseMapper.menuByRoleId(roleId, -1);
        // 根据父级菜单查询对应的子菜单
        for (Menu root : rootMenu) {
            List<Menu> children = baseMapper.menuByRoleId(roleId, root.getMenuId());
            root.setChildren(children);
        }
        return rootMenu;
    }
}
